/*
 * 监控区主页的主区域, 设置, 播放列表
 * 命名空间 "setting-playList"
 * 
 * @author Zdream
 * @since 0.2.0
 */

monitor.ui.body.addContent({
	name : "setting-playList",
	
	init : function (argv) {
		this.state = {
			allPlayLists : [],
			projects : [], // 当前选择的播放列表的所有 project. 每一项所含的元素有 pid, pname
			current : {
				state : "check",
				selected : -1, // 选择的播放列表的 lid, 未选择时, 或者添加新的播放列表时, 为 -1
				selectedIndex : -1 // 选择的 lid 在 allPlayLists 的索引. 无效时为 -1
			}
		};
		
		var allPlayLists = monitor.store.queryAllPlayLists();
		if (allPlayLists != null) {
			allPlayLists.forEach(item => this.state.allPlayLists.push(item));
		}
		
		// monitor.store
		// vm create
		var thiz = this;
		this.vm = new Vue({
			el: "#mnt_body",
			data: this.state,
			methods: {
				selectPlayList : function (index) {
					thiz.selectPlayList(index);
				}
			}
		});
		
		// 消息监听
		monitor.msgCenter.subscribe(
				["query-data-all-playList", "query-data-project"],
		function(event, argv) {
			switch (event) {
			case "query-data-all-playList":
			{
				thiz.handleQueryAllPlayLists(argv);
			} break;
			case "query-data-project":
			{
				thiz.handleQueryProject(argv);
			} break;
			}
		}, this.name);
	},
	
	/**
	 * 消息触发
	 */
	handleQueryAllPlayLists : function (data) {
		var array = this.state.allPlayLists;
		array.splice(0, array.length);
		
		data.forEach(item => array.push(item));
		this.scanSelected();
		this.vm.$forceUpdate();
	},
	
	/**
	 * 消息触发
	 */
	handleQueryProject : function (data) {
		var pid = data.pid;
		var pname = data.name;
		
		// playList
		var array = this.state.projects;
		var length = array.length;
		for (var i = 0; i < length; i++) {
			if (array[i].pid == pid) {
				array[i].pname = pname;
			}
		}
	},
	
	/**
	 * 当播放列表 allPlayLists 发生变动之后,
	 * 修正一下选择查看、修改的播放列表是否有效
	 */
	scanSelected : function () {
		var selected = this.state.current.selected;

		this.state.projects.splice(0, this.state.projects.length);
		if (selected == -1) {
			this.state.current.selectedIndex = -1;
			return;
		}
		
		var array = this.state.allPlayLists;
		var length = array.length;
		var found = false;
		for (var i = 0; i < length; i++) {
			if (array[i].lid == selected) {
				this.state.current.selectedIndex = i;
				found = true;
				break;
			}
		}
		
		if (!found) {
			this.state.current.selectedIndex = -1;
			this.state.current.selected = -1;
		} else {
			// 更新 this.state.projects
			var pids = array[this.state.current.selectedIndex].projects;
			length = pids.length;
			for (var i = 0; i < length; i++) {
				var pid = pids[i];
				var pname = pid;
				var project = monitor.store.queryProject(pid);
				if (project != null) {
					pname = project.name;
				}
				this.state.projects.push({
					pid : pid,
					pname : pname
				});
			}
		}
	},
	
	/**
	 * 按钮触发
	 */
	selectPlayList : function (index) {
		this.state.current.selectedIndex = index;
		this.state.current.selected = this.state.allPlayLists[index].lid;
		this.scanSelected();
	},
	
	destroy : function () {
		this.state = null;
		
		// 删除消息监听
		monitor.msgCenter.remove(this.name);
		
		// vm destroy
		if (this.vm != null)
			this.vm.$destroy();
		this.vm = null;
	}
	
});